<?
include_once("ANotify.class.php");
class Warmy_GD extends ANotify
{
	function Check_Color(& $Color)
	{
		if(!eregi("^#?[0-9a-f]{6}$",$Color))
			$this->halt("颜色的参数必须为十六进制");
		return substr($Color,-6);
	}
	function RGB2HSB($color)
	{
		$s=$h=$l=0;
		$r=$g=$g=0;
		//H
		$r	= hexdec(substr($color,0,2));
		$g	= hexdec(substr($color,2,2));
		$b	= hexdec(substr($color,4,2));

		$Dif=$CCmax=$CCmin=$RC=$GC=$BC=$TempH=$TempS=$TempL = 0.0;         

		$RC = $r / 255.0;
		$GC = $g / 255.0;
		$BC = $b / 255.0;
		$CCmax = max($RC,$GC,$BC);
		$CCmin = min($RC,$GC,$BC);

		$TempL = ($CCmax + $CCmin) / 2.0;
		if ($CCmax == $CCmin)
		{
			$TempS = 0;
			$TempH = 0;
		}
		else
		{
			$Dif = $CCmax - $CCmin;   

			if ($TempL < 0.5)
				$TempS = $Dif / ($CCmax + $CCmin);
			else
				$TempS = $Dif / ( 2.0 - $CCmax - $CCmin );   

			if ($RC == $CCmax)
				$TempH = ($GC - $BC) / $Dif;
			else
				if ($GC == $CCmax)
					$TempH = 2.0 + ($BC - $RC) / $Dif;
				else
					$TempH = 4.0 + ($RC - $GC) /$Dif;

			$TempH = $TempH / 6;
			if ($TempH < 0) 
				$TempH = $TempH + 1;

		}

		$H = Round( 240 * $TempH );
		$S = Round( 240 * $TempS );
		$L = Round( 240 * $TempL ); 
		return array($H ,$S, $L);

	}
	function hsb3rgb($hsl)
	{
		return $this->HSB2RGB($hsl[0],$hsl[1],$hsl[2]);
	}
	function HSB2RGB($H,$S,$L)
	{
		$HN=$SN=$LN=$RD=$GD=$BD=$V=$M=$SV=$Fract=$VSF=$Mid1=$Mid2 = 0.0; 
		$R=$G=$B= 0;
		$Sextant=0;
		$HN = $H / 239;
		$SN = $S / 240;
		$LN = $L / 240;

		if ($LN < 0.5)
			$V = $LN * ( 1.0 + $SN );
		else
			$V = $LN + $SN - $LN * $SN;

		if ($V <= 0)
		{
			$RD = 0.0;
			$GD = 0.0;
			$BD = 0.0;
		}
		else
		{
			$M = $LN + $LN - $V;
			$SV = ($V - $M ) /$V;
			$HN = $HN * 6.0;
			$Sextant = round($HN);
			$Fract = $HN - $Sextant;
			$VSF = $V * $SV * $Fract;
			$Mid1 = $M + $VSF;
			$Mid2 = $V - $VSF;
			switch  ($Sextant)
			{
				case 0:
				{
					$RD = $V;
					$GD = $Mid1;
					$BD = $M;
					break;
				}
				case 1:
				{
					$RD = $Mid2;
					$GD = $V;
					$BD = $M;
					break;
				}
				case 2:
				{
					$RD = $M;
					$GD = $V;
					$BD = $Mid1;
					break;
				}
				case 3:
				{       
					$RD = $M;
					$GD = $Mid2;
					$BD = $V;
					break;
				}
				case 4:
				{
					$RD = $Mid1;
					$GD = $M;
					$BD = $V;
					break;
				}
				case 5:
				{
					$RD = $V;
					$GD = $M;
					$BD = $Mid2;
					break;
				}
				default:
				{
					$RD = $V;
					$GD = $Mid1;
					$BD = $M;
					break;
				}
			}
		}
		if ($RD > 1.0)      $RD = 1.0;
		if ($GD > 1.0)      $GD = 1.0;
		if ($BD > 1.0)      $BD = 1.0;

		$R = dechex(Round( $RD * 255 ));
		$G = dechex(Round( $GD * 255 ));
		$B = dechex(Round( $BD * 255 ));           
		$R	= substr("00$R",-2);
		$G	= substr("00$G",-2);
		$B	= substr("00$B",-2);
		$ret=strtoupper("$R$B$B");
		return $ret;
	}

	function RGB2HSL($color)
	{
		$PIE_VALUE	= 3.1415926535;
		$nHValue	= 0;
		$nSValue	= 0;
		$nLValue	= 0;
		//
		//PRINT("$color<br>");
		$R	= hexdec(substr($color,0,2));
		$G	= hexdec(substr($color,2,2));
		$B	= hexdec(substr($color,4,2));
//print("R=$R<BR>B=$G<BR>B=$B<BR>");
		$R	= $R/255.0;
		$G  = $G/255.0;
		$B	= $B/255.0;
		$max=$min=$diff=$r_dist=$g_dist=$b_dist=0.0;
		$H=$L=$S = 0;

		$max = max($R,$G,$B);
		$min = min($R,$G,$B);
		$diff = $max-$min;
		$L		= ($max+$min)/2;
		//PRINT("L=$L<br>");
		if ($diff==0) 
		{
			$H	= 0;
			$S	= 0;
		} 
		else 
		{
			if ($L<0.5) 
				$S = $diff/($max+$min);
			else 
				$S = $diff/(2-$max-$min);      
			$r_dist = ($max-$R)/$diff;
			$g_dist = ($max-$G)/$diff;
			$b_dist = ($max-$B)/$diff;
			if ($R == $max) 
				$H = $b_dist-$g_dist; 
			else if ($G == $max) 
				$H = 2+$r_dist-$b_dist; 
			else if ($B == $max) 
				$H = 4+$g_dist-$r_dist;
			$H *= 60;
			if ($H<0) 
				$H += 360;
			if ($H>=360) 
				$H -= 360;
		}
		//$H	= round($H);
		//$S	= round($S*100);
		//$L	= round($L*100);
		//print("H=$H<BR>S=$S<BR>L=$L<BR>");
		return array($H ,$S, $L);

	}
	function HSL2RGB($H,$S,$L)
	{
		$p1 = $p2 = 0.0;
		$R = $G = $B = 0;
		if ($L<=0.5) 
			$p2	= $L*(1+$S);
		else 
			$p2	= $L+$S-($L*$S);
		$p1	= 2*$L-$p2;
		if ($S==0) 
		{
			$R	= $L; 
			$G	= $L;
			$B	= $L;
		} 
		else 
		{
			$R = $this->RGB($p1,$p2,$H+120.0);
			$G = $this->RGB($p1,$p2,$H);
			$B = $this->RGB($p1,$p2,$H-120.0);
		}
		//PRINT("R=$R<BR>G=$G<BR>B=$B<BR>");
		$R = dechex(Round( $R * 255 ));
		$G = dechex(Round( $G * 255 ));
		$B = dechex(Round( $B * 255 ));    
		//PRINT("R=$R<BR>G=$G<BR>B=$B<BR><br><br>");
		//PRINT("R=$R<BR>G=$G<BR>B=$B<BR>");
		$R	= substr("00$R",-2);
		$G	= substr("00$G",-2);
		$B	= substr("00$B",-2);
		$ret=strtoupper("$R$G$B");
		return $ret;
	}
	function RGB($q1,$q2,$hue) 
	{
		if ($hue>360) 
			$hue=$hue-360.0;
		if ($hue<0) 
			$hue=$hue+360.0;
		if ($hue<60) 
			return ($q1+($q2-$q1)*$hue/60.0);
		else if ($hue<180) 
			return($q2);
		else if ($hue<240) 
			return($q1+($q2-$q1)*(240-$hue)/60.0);
		else 
			return($q1);
	}
}
?>